package com.our.base.easylog.logback;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.PatternLayout;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.classic.layout.TTLLLayout;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
import ch.qos.logback.core.joran.spi.JoranException;
import com.our.base.easylog.IPushLog;
import com.our.base.easylog.Initializer;
import com.our.base.easylog.config.EasyLogProperties;
import com.our.base.easylog.config.es.EsProperties;
import com.our.base.easylog.constants.PatternConstants;
import com.our.base.easylog.es.EsPushLogImpl;
import com.our.base.easylog.logback.appender.PushLogAppender;
import com.our.base.easylog.utils.Banner;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.StringUtils;

import java.net.URL;
import java.util.Map;
import java.util.Objects;

/**
 * @author poison
 * @Description logback 初始化实现 （在spring boot 初始化之前）
 * @date 2023/12/6
 **/

@Slf4j(topic = "easy-log")
public class LogbackInitializer extends Initializer {



    public LogbackInitializer(String pattern) {

        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

        JoranConfigurator configurator = new JoranConfigurator();

        String springLogbackFile = "logback-spring.xml";
        String logbackFilePath = "logback.xml";
        configurator.setContext(loggerContext);
        URL springLogbackUrl = getClass().getClassLoader().getResource(springLogbackFile);
        URL logbackUrl = getClass().getClassLoader().getResource(logbackFilePath);
        try {
            if (springLogbackUrl != null) {
                configurator.doConfigure(springLogbackUrl);
            } else if (logbackUrl != null) {
                configurator.doConfigure(logbackUrl);
            } else {
                log.error("没有找到logback配置文件");
            }
        } catch (JoranException exception) {
            log.error("没有正确加载logback配置文件:" + exception.getMessage());
        }


        PatternLayout layout = new PatternLayout();
        layout.setPattern(pattern);
        layout.setContext(loggerContext);
        layout.start();
        PushLogAppender pushLogAppender = new PushLogAppender();
        pushLogAppender.setContext(loggerContext);
        pushLogAppender.setName("logPush");
        pushLogAppender.setLayout(layout);
        pushLogAppender.start();

        ch.qos.logback.classic.Logger logger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
        logger.setLevel(Level.INFO);
        logger.addAppender(pushLogAppender);
        setInitializerClassName(pushLogAppender.getClass().getName());
        log.info("Easy-logLogBack日志拦截器(Appender)已加入成功。");


    }


    @Override
    public String getInitializerClassName() {
      return initializerClassName;
    }
}
